import api from '../../api/api'
import Toast from '@vant/weapp/toast/toast'
Page({
  data: {
    swiperData: [],
    name: '',
    hotData: []
  },
  async searchHandler(e) {
    let name = e.detail.value.name
    this.setData({
      name
    })
    if (name == '') {
      this.getHot()
    }
    const res = await api.search({ name })
    if (res.data.code == "Q001") {
      if (res.data.result.length > 0) {
        this.setData({
          hotData: res.data.result
        })
      } else {
        this.setData({
          hotData: []
        })
      }
    }
    console.log('搜索', res)
  },
  toDetail(e) {
    let pid = e.currentTarget.dataset.pid
    wx.navigateTo({
      url: '/pages/detail/detail?pid=' + pid
    })
  },
  async getSwiperData() {
    const res = await api.getSwiper()
    console.log('轮播图', res)
    this.setData({
      swiperData: res.data.result
    })
  },
  async getHot() {
    let params = {
      key: 'isHot',
      value: 1
    }
    const res = await api.getGoodsByType(params)
    console.log('类型获取商品', res)
    this.setData({
      hotData: res.data.result
    })
  },
  onLoad: function () {

  },
  onShow: function () {
    this.getSwiperData()
    this.getHot()
  }
})